package com.newbye.component
{
	import com.newbye.component.ComponentInstanceItem;
	
	import flash.display.DisplayObject;
	import flash.utils.getDefinitionByName;
	
	import mx.controls.Alert;

	public class ComponentFactory
	{
		public static function createComponent(className:String):DisplayObject
		{
			var dis:DisplayObject;
			
			try
			{
				var cls:Class = getDefinitionByName(className) as Class;
				dis = new cls();
			}
			catch(e:*)
			{
				Alert.show("Class '" + className + "' 未定义或定义有误!");
			}
			
			return dis;
		}
		
		public static function createComponentInstanceItem(className:String):ComponentInstanceItem
		{
			var citem:ComponentInstanceItem;
			
			var dis:DisplayObject = createComponent(className);
			if(null != dis)
			{
				citem = new ComponentInstanceItem(className, dis);
				if(dis.hasOwnProperty("text"))
				{
					dis["text"] = "default text";
				}
			}
			
			return citem;
		}
	}
}